package com.itcast.demo.validate.controller;

import com.itcast.demo.validate.pojo.UserGroup;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/validate")
public class ValidateController2 {

    // 分组校验
    /**
     * 注意：  在上面的示例中，createUser方法验证userid和name字段，
     * 而updateUser方法验证name和email字段。这是通过为@Validated注解指定不同的验证分组来实现的
     * @param user
     * @return
     */

    @PostMapping("/create")
    public ResponseEntity<String> createUser(@RequestBody @Validated(UserGroup.GroupA.class) UserGroup user) {
        // 创建用户逻辑
        return ResponseEntity.ok("用户创建成功");
    }

    @PostMapping("/update")
    public ResponseEntity<String> updateUser(@RequestBody @Validated(UserGroup.GroupB.class) UserGroup user) {
        // 更新用户逻辑
        return ResponseEntity.ok("用户更新成功");
    }
}


